package com.lgx.mall.business.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.mall.business.dao.IOrderDetailDao;
import com.lgx.mall.business.entity.OrderDetail;
import com.lgx.mall.core.entity.Category;

/**
 * 
 * @author acer
 *
 */
@Component
public class OrderDetailDao extends GeneralDao implements IOrderDetailDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	@Transactional
	public OrderDetail createOrderDetail(OrderDetail detail)
			throws DataException {
		this.getHibernateTemplate().save(detail);
		log.info("[新增OrderDetail]:"+detail.getId());
		return detail;
	}

	@Override
	@Transactional
	public OrderDetail updateOrderDetail(OrderDetail detail)
			throws DataException {
		this.getHibernateTemplate().update(detail);
		log.info("[更新OrderDetail]:"+detail.getId());
		return detail;
	}

	@Override
	@Transactional
	public void deleteOrderDetail(OrderDetail detail) throws DataException {
		log.info("[删除OrderDetail]:"+detail.getId());
		this.getHibernateTemplate().delete(detail);
	}

	@Override
	public void deleteOrderDetail(long id) throws DataException {
		this.deleteOrderDetail(this.findOrderDetailById(id));
	}

	@Override
	public OrderDetail findOrderDetailById(long id) {
		return this.getHibernateTemplate().get(OrderDetail.class, id);
	}

	/**
	 * 根据订单id查找这个id对应的订单明细信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<OrderDetail> findOrderDetailList(long orderId) {
		String sql = "from OrderDetail od where od.order.id = " + orderId + " order by od.id asc";
		return this.getHibernateTemplate().find(sql);
	}

	@Override
	public OrderDetail findOrderDetailById(long orderId, String entityId,
			String entityType) {
		String sql = "from OrderDetail od where od.order.id = " + orderId + "";
		
		sql += " and od.entityId = " + entityId;
		
		sql += " and od.entityType = '" + entityType + "'";
		
		@SuppressWarnings("unchecked")
		List<OrderDetail> details = this.getHibernateTemplate().find(sql);
		if(details.size() > 0) return details.get(0);
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String checkOrderDetail(long orderId, long productId) {
		//获取category
		String hql = "select p.category from Product p where p.id = " + productId;
		List<Category> categorys = this.getHibernateTemplate().find(hql);
		
		String sql = " select * from tm_order_detail od " + 
				 " inner join tm_product p on od.entityId = p.id  " +
				 " where od.order_id = " + orderId +
				 " and od.entityType = 'com.lgx.mall.core.entity.Product' " +
				 " and p.category_id = " + categorys.get(0).getId();
		
		List<OrderDetail> list = (List<OrderDetail>) this.baseDao.queryJdbcForList(sql, OrderDetail.class);
		
		int count = 0;
		
		for(OrderDetail detail : list){
			count += detail.getAmount();
		}
		
		if(categorys.get(0).getPcount() != -1)
		{
			if(count>categorys.get(0).getPcount()){
				//超出了同一类产品的购卖数量
				return "3";
			}else{
				//有重复的
				return "2";
			}
			
		}else{
			if(count > 1){
				//有重复的
				return "2";
			}else{
				//没有重复的
				return "1";
			}
		}
	}

}
